<template>
  <div class="box">
    <h1>登录页面</h1>
    <div>
      <el-form :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="用户名">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密 码">
          <el-input v-model="form.userPass" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { login } from "@/api/api";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
// do not use same name with ref
const form = ref({
  userName: null,
  userPass: null,
});

const onSubmit = () => {
  console.log("submit!");
  login(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
      sessionStorage.setItem("token", res.data.data.token);
      sessionStorage.setItem("user", JSON.stringify(res.data.data));
      router.push("/home");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
</script>

<style scoped></style>
